package com.hmt.pharmacy.module.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmt.pharmacy.api.medicine.SearchMedicineParamDto;
import com.hmt.pharmacy.api.medicine.SearchMedicineSelectParamDto;
import com.hmt.pharmacy.module.medicine.entity.Medicine;
import com.hmt.pharmacy.module.medicine.mapper.MedicineMapper;
import com.hmt.pharmacy.module.medicine.service.MedicineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import freemarker.template.utility.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 钟志豪
 * @since 2025-06-25
 */
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {


    @Override
    public Page<Medicine> findPage(SearchMedicineParamDto search) {
        Page<Medicine> page = search.toPage();
        LambdaQueryWrapper<Medicine> lambda = new QueryWrapper<Medicine>().lambda();
        if (StringUtils.isNotBlank(search.getCode())) {
            lambda.like(Medicine::getCode, search.getCode());
        }
        if (StringUtils.isNotBlank(search.getEndcode())) {
            lambda.like(Medicine::getEndcode, search.getEndcode());
        }
        if (StringUtils.isNotBlank(search.getName())) {
            lambda.like(Medicine::getName, search.getName());
        }
        if (StringUtils.isNotBlank(search.getSpec())) {
            lambda.like(Medicine::getSpec, search.getSpec());
        }
        if (StringUtils.isNotBlank(search.getUnit())) {
            lambda.like(Medicine::getUnit, search.getUnit());
        }
        if (StringUtils.isNotBlank(search.getPrescribeUnit())) {
            lambda.like(Medicine::getPrescribeUnit, search.getPrescribeUnit());
        }
        if (StringUtils.isNotBlank(search.getNactionInsurCode())) {
            lambda.like(Medicine::getNactionInsurCode, search.getNactionInsurCode());
        }
        page = baseMapper.selectPage(page, lambda);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveData(Medicine entity) {
        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateData(Medicine entity) {
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public Page<Medicine> findSelectList(SearchMedicineSelectParamDto search) {
        Page<Medicine> page = search.toPage();
        LambdaQueryWrapper<Medicine> lambda = new QueryWrapper<Medicine>()
                .lambda();
        if (StringUtils.isNotBlank(search.getFilterText())) {
            lambda.and(wrapper -> {
               wrapper.like(Medicine::getCode, search.getFilterText())
                       .or().like(Medicine::getEndcode, search.getFilterText())
                       .or().like(Medicine::getName, search.getFilterText())
                       .or().like(Medicine::getNactionInsurCode, search.getFilterText());
            });
        }
        page = baseMapper.selectPage(page, lambda);
        return page;
    }
}
